package com.nl.controller.web.controller;

import com.github.pagehelper.PageInfo;
import com.nl.controller.common.model.Response;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.service.SmsUserService;
import com.nl.controller.web.bean.SmsRule;
import com.nl.controller.web.bean.SmsUsers;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user")
@Slf4j
public class SmsUserConroller {
    @Autowired
    private SmsUserService smsUserService;

    @ApiOperation(value = "用户管理模板", notes = "分页查询用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "String"),
            @ApiImplicitParam(name = "msisdn", value = "手机号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "name", value = "用户姓名", required = true, dataType = "String"),
    })
    @PostMapping("find")
    public Response findUser(
            @RequestParam(value = "page", required = true) int page,
            @RequestParam(value = "limit", required = true) int limit,
            @RequestParam(value = "msisdn", required = false) String msisdn,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam Map<String, Object> paramMap
    ) {
        PageInfo<SmsUsers> pageInfo = smsUserService.findUser(page, limit, msisdn, name);
        log.info("用户查询：{}", paramMap.toString());
        log.debug("用户查询统计返回数据：{}", pageInfo.getList().toString());
        return ResponseUtil.sucess(pageInfo.getList(), pageInfo.getTotal());
    }

    @ApiOperation(value = "用户管理模板", notes = "查询角色信息")
    @PostMapping("findRule")
    public Response<SmsRule> findOperatorByCityId( @RequestParam(value = "duty") String duty) {
        List<SmsRule> list = smsUserService.findRule(duty);
        log.info("查询角色信息：{}");
        log.debug("查询角色信息返回参数：{}",list.size());
        return ResponseUtil.sucess(list);
    }


    @ApiOperation(value = "用户管理模板", notes = "添加用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "msisdn", value = "手机号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "userName", value = "用户姓名", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "duty", value = "等级", required = true, dataType = "String"),
            @ApiImplicitParam(name = "cityId", value = "城市编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "countyId", value = "区县编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "mgrId", value = "项目编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "roleId", value = "角色编号", required = true, dataType = "String")
    })
    @PostMapping("add")
    public int addUser(
            @RequestParam(value = "msisdn") String msisdn,
            @RequestParam(value = "userName") String userName,
            @RequestParam(value = "password") String password,
            @RequestParam(value = "duty") String duty,
            @RequestParam(value = "cityId") String cityId,
            @RequestParam(value = "countyId") String countyId,
            @RequestParam(value = "mgrId") String mgrId,
            @RequestParam(value = "roleId") String roleId
    ) {
        return smsUserService.addUser(msisdn, userName, password, duty, cityId,countyId, mgrId,roleId);
    }

    @ApiOperation(value = "用户修改模板", notes = "查询单个用户信息")
    @PostMapping("findUserByUserId")
    @ApiImplicitParam(name = "userId", value = "userId", required = true, dataType = "String")
    public SmsUsers findUserByUserId(
            @RequestParam(value = "userId") String userId) {
        SmsUsers smsUsers = new SmsUsers();
        smsUsers = smsUserService.findUserByUserId(userId);
        log.info("查询单个用户参数：{}", userId);
        log.debug("查询单个用户返回数据：{}", smsUsers.toString());
        return smsUsers;
    }

    @ApiOperation(value = "用户修改模板", notes = "修改用户的信息")
    @PostMapping("modify")
    public int modifyUser(
            @RequestParam(value = "userId") String userId,
            @RequestParam(value = "msisdn") String msisdn,
            @RequestParam(value = "userName") String userName,
            //@RequestParam(value = "password") String password,
            @RequestParam(value = "duty") String duty,
            @RequestParam(value = "cityId") String cityId,
            @RequestParam(value = "countyId") String countyId,
            @RequestParam(value = "mgrId") String mgrId,
            @RequestParam(value = "roleId") String roleId,
            @RequestParam Map<String, Object> paramMap
    ) {
        int result = smsUserService.modifyUser(userId,msisdn, userName, duty, cityId, countyId, mgrId, roleId);
        log.info("用户修改参数：{}", paramMap.toString());
        log.debug("用户修改返回数据：{}", result);
        return result;
    }

    @ApiOperation(value = "用户删除模板", notes = "删除用户的信息")
    @PostMapping("remove")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String")
    public int deleteUser(@RequestParam(value = "userId") String userId) {
        int result= smsUserService.deleteUser(userId);
        log.info("用户删除参数：{}", userId);
        log.debug("用户删除返回数据：{}", result);
        return result;
    }
}
